String.prototype.replaceAll = function ( reallyDo, replaceWith, ignoreCase ) {
    if ( !RegExp.prototype.isPrototypeOf( reallyDo ) ) {
        return this.replace( new RegExp( reallyDo, (ignoreCase ? "gi" : "g") ), replaceWith );
    } else {
        return this.replace( reallyDo, replaceWith );
    }
}
Date.prototype.format = function ( format ) {
    var o = {
        "M+":this.getMonth() + 1, //month
        "d+":this.getDate(), //day
        "h+":this.getHours(), //hour
        "m+":this.getMinutes(), //minute
        "s+":this.getSeconds(), //second
        "q+":Math.floor( (this.getMonth() + 3) / 3 ), //quarter
        "S":this.getMilliseconds() //millisecond
    }
    if ( /(y+)/.test( format ) ) format = format.replace( RegExp.$1, (this.getFullYear() + "").substr( 4 - RegExp.$1.length ) );
    for ( var k in o ) if ( new RegExp( "(" + k + ")" ).test( format ) )
        format = format.replace( RegExp.$1,
            RegExp.$1.length == 1 ? o[k] :
                ("00" + o[k]).substr( ("" + o[k]).length ) );
    return format;
}

if ( typeof(mclog) == "undefined" ) {
    var mclog = {};
}

mclog.include = function ( file ) {
    var files = typeof file == "string" ? [file] : file;
    for ( var i = 0; i < files.length; i++ ) {
        var name = files[i].replace( /^\s|\s$/g, "" );
        var att = name.split( '.' );
        var ext = att[att.length - 1].toLowerCase();
        var isCSS = ext == "css";
        var tag = isCSS ? "link" : "script";
        var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
        var link = (isCSS ? "href" : "src") + "='" + url + "/" + name + "'";
        if ( $( tag + "[" + link + "]" ).length == 0 ) document.write( "<" + tag + attr + link + "></" + tag + ">" );
    }
}

mclog.dialog = function( options ){
    $.jBox( "<div style='padding: 10px;'>" + options['content'] + "</div>", { title: options['title'], width: 'auto' } );
}
mclog.validate = function( f ){
    var flag = true;
    var form = f || $( "form input,form textarea" );
    var show = function ( obj, msg ) {
        msg = msg || (obj.attr( 'placeholder' ) || obj.attr( 'title' ));
        obj.tooltip( { title: msg, placement: 'bottom', trigger: 'manual'} );
        obj.tooltip( 'show' );
        window.setTimeout(function(){ obj.tooltip( 'destroy' ); }, 3200 );
        obj.focus();
    };
    var required = function( value, obj ){ if( value.length <= 0 ){ show( obj ); return false; } return true; } ;
    var username = function( value, obj ){ if( value.search( /^(\w|-){3,15}$/ ) == -1) { show( obj, '只能输入3~15个字母，数字和下划线!' );return false; } return true;};
    var email = function( value ,obj ){ if( value.length > 0 && value.search( /^([\w\-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})$/ ) == -1) { show( obj, '邮箱地址不正确!' );return false; } return true; };
    var match = function ( value, obj ) { var p = "/" + obj.attr( 'match' ) + "/"; if ( value.search( eval( p ) ) == -1 ) { show( obj, obj.attr('tip') ); return false; } return true; };
    var number = function( value, obj ){ if( value.search( /^([0-9]*)$/ ) == -1 ){ show( obj, '只能是数字!' ); return false; } return true; };
    var identical = function () {
        var aa = $.trim( $( '.identical:eq(0)' ).val() );
        var bb = $.trim( $( '.identical:eq(1)' ).val() );
        if ( aa == bb ) {  return true; }
        show( $( '.identical:eq(1)' ), '2次输入不一致!' );
        return false;
    }
    form.each(function(){
        var obj = $( this );
        var value = $.trim( obj.val() );
        if ( obj.attr( 'match' ) ) { flag = match( value, obj );if ( !flag ) return false; }
        if ( obj.hasClass( 'required' ) ) { flag = required( value, obj );if ( !flag ) return false; }
        if ( obj.hasClass( 'username' ) && value.length > 0 ) { flag = username( value, obj );if ( !flag ) return false; }
        if ( obj.hasClass( 'identical' ) ) { flag = identical();if ( !flag ) return false; }
        if ( obj.hasClass( 'number' ) && value.length > 0 ) { flag = number( value, obj );if ( !flag ) return false; }
        if ( obj.hasClass( 'email' ) ) { flag = email( value, obj );if ( !flag ) return false; }
    });
    return flag;
}
mclog.tip = function( content, status ){
    status =  status || 'info';
    var opacity = status == 'loading' ? 0.5 : 0.2;
    $.jBox.tip( content, status, { opacity: opacity } );
}

mclog.success = function ( content ) {
    mclog.tip( content, 'success' );
}

mclog.loading = function ( content ) {
    mclog.tip( content, 'loading' );
}

mclog.closeTip = function(){
    $.jBox.closeTip();
}

mclog.ajaxGET = function( obj, fun ) {
    var url = obj.attr( 'href' );
    var text = obj.attr( 'title' ) || obj.html();
    var referer = obj.attr( 'rel' ) || false;

    mclog.loading( '正在' + text + '...' );

    $.get( url + "&ajax=1", function ( result ) {
        if( fun ){ fun( result ); return; }
        if ( result == 'success' ) {
            window.setTimeout( function () { mclog.success( text + '完成' ); }, 2000 );
            window.setTimeout( function () { if ( referer ) { window.location.href = referer; } else{ window.location.reload(); }  }, 3000 );
            return;
        }
        window.setTimeout( function(){ mclog.closeTip(); mclog.dialog( {content: result, title: text + '出错'} ); }, 2000 );
    });
}

mclog.ajaxPOST = function( obj, fun, debug ) {

    var form = obj.parents( 'form' );

    if ( !mclog.validate( form.find( 'input,textarea' ) ) ) { return false; }

    var tip = obj.attr( 'title' ) || obj.html();
    var referer = obj.attr( 'href' );

    mclog.loading( '正在' + tip + '...' );

    if ( typeof editor1 == 'object' ) {
        editor1.sync();
    }
    if ( typeof editor2 == 'object' ) {
        editor2.sync();
    }

    var url = form.attr('action');
    var date = form.serialize();

    if ( debug ) { form.submit(); return; }

    $.post( url + "&ajax=1", date, function ( result ) {
        if( fun ){ fun( result ); return; }
        if ( result == 'success' ) {
            window.setTimeout( function(){ mclog.success( tip + '成功!' );}, 2000 );
            window.setTimeout( function () { if ( referer )  window.location.href = referer; else window.location.reload(); }, 3000 );
            return;
        }
        window.setTimeout( function(){ mclog.closeTip(); mclog.dialog( {content: result, title: text + '出错'} ); }, 2000 );
    });
}

mclog.include( ['static/css/bootstrap.min.css', 'static/css/jbox.css', 'static/js/jbox.min.js', 'static/js/bootstrap.min.js'] );